<template>
    <div class="wrap">
        <div class="zhihui">
            <div>智慧小区信息平台</div>
            
        </div>
        
        <div class="inner-wrap">
            <van-field
            readonly
            clickable
            name="picker"
            :value="villages"
            label="身份"
            placeholder="点击选择"
            @click="villagePicker = true"
            />
            <van-button @click="enter" class="btn" type="primary" block>进入系统</van-button>
        </div>
        <van-popup v-model="villagePicker" position="bottom">
            <van-picker
                show-toolbar
                :columns="villagecolumns"
                @confirm="villageConfirm"
                @cancel="villagePicker = false"
            />
        </van-popup>
    </div>
</template>

<script>
import { Notify, Toast } from 'vant';
import api from '../api/type.js'
    export default {
        data: function () {
            return {
                subscribe:'',
                data:{},
                village:'',
                villagecolumns:[{
                    type:1,
                    text:'常住人口'
                },{ type:2,
                    text:'访客'}],
                villagePicker:false,
                villages:'',
            }
        },
        created(){
           if(!openid){
                this.getOpenID()
            }else{
                Toast.loading({
                    message: '系统初始化...',
                    forbidClick: true,
                });
                let sn=getQueryVariable('sn')
                if(sn){
                   
                    this.$router.replace({path:'opendoor',query:{sn}})
                    return
                }
                api.getRole({openId:openid}).then(e=>{
                    Toast.clear()
                    sessionStorage.setItem('isOwner',e.data.isOwner)
                    sessionStorage.setItem('isCheck',e.data.isCheck?e.data.isCheck:'no')
                    if(e.data.isOwner==1&&e.data.isCheck){
                        this.$router.replace('/home')
                    }else{

                    }
                }).catch(err=>{
                    Toast.clear()
                    console.log(err)
                })
            } 
        },
        mounted() {
           
        },

        methods: {
            getCode(){
                api.getCode().then(e=>{
                    Toast.clear()
                   
                    console.log(e)
                   
                }).catch(err=>{
                   Toast.clear()
                    console.log(err.response.data)
                })
            },
            getOpenID(){
                Toast.loading({
                    message: '系统初始化...',
                    forbidClick: true,
                });
                let that=this
                let code=getQueryVariable('code')
               
                api.getOpenID({code}).then(e=>{
                    Toast.clear()
                    openid=e.data.openId
                    
                    that.subscribe=e.data.subscribe
                    if(e.data.subscribe==0){
                        this.$router.replace({ path: 'guanzhu' })
                    }else{
                        
                        setCookie('openid',openid,123)
                        let sn=getQueryVariable('sn')
                        if(sn){
                            this.$router.replace({path:'opendoor',query:{sn}}) 
                        }
                    }
                    
                    
                }).catch(err=>{
                   Toast.clear()
                    console.log(err.response.data)
                })
            },
            enter(){
                if(!openid){
                    Toast('系统初始化失败')
                    return;
                }
                if(this.village){
                    if(this.village==1){
                        this.$router.push({path:'/first'})
                    }else{
                        this.$router.push('/village')
                    }
                    
                    
                }else{
                    Toast('请选择身份')
                }
                
            },
            villageConfirm(val){
               this.village=val.type
               sessionStorage.setItem('type',val.type)
               this.villages=val.text
               this.villagePicker=false
            }
           
        }
    }

</script>
<style lang="scss">
@import "../common.scss";
.now-title{
    color:#fff;
    margin-top:10px;
    margin-bottom:15px;
    font-size:px2rem(10px)
}
.num-item{
    color:#fff;
        font-size:px2rem(10px)
    }
</style>
<style lang="scss" scoped>
@import "../common.scss";
    
    .zhihui{
        position:fixed;
        text-align: center;
        top:15vh;
        width:100%;
        font-size:px2rem(16px);    }
    .wrap{
        height:100vh;
        background:url(./bg.png);
        background-size:100% 100%;
        display:flex;
        align-items: center;
    }
    .inner-wrap{
        width:80%;
        margin:0 auto;
    }
    .btn{
        margin-top:42px;
    }
</style>